package com.androidme.app.framework.base;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public abstract class AndroidMeListActivity<T> extends AndroidMeActivity implements AbsListView.OnScrollListener {
    private ArrayList<T> inerEntries = new ArrayList<T>();
    protected ListView listView;
    protected View footView;
    protected View headView;

    private DataAdapter dataAdapter;
    private boolean shouldRefresh = false;
    private boolean isRefreshing = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (dataAdapter == null) {
            listView = (ListView) findViewById(android.R.id.list);
            dataAdapter = new DataAdapter();
        }
        addListHeader();
        addListFooter();
        listView.setAdapter(dataAdapter);
        listView.setOnScrollListener(this);
    }

    @Override
    protected void onRefresh() {
        isRefreshing = false;
        dataAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        inerEntries.clear();
    }

    public T getData(int position) {
        if (position >= inerEntries.size()) return null;
        else return inerEntries.get(position);
    }


    public void addDatas(List<T> datas) {
        if (datas == null || datas.size() == 0) return;

        for (T data : datas) {
            addData(data);
        }
    }

    public void addData(T t) {
        if (t == null) return;

        boolean needAdd = true;
        for (T data : inerEntries) {
            if (isDataSame(data, t)) {
                needAdd = false;
            }
        }
        if (needAdd) {
            inerEntries.add(t);
        }
    }

    public void clearData() {
        inerEntries.clear();
    }

    public void removeData(int position) {
        inerEntries.remove(position);
    }

    public int getDataCount() {
        return inerEntries.size();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (shouldRefresh && scrollState == SCROLL_STATE_IDLE) {
            if (!isRefreshing) {
                isRefreshing = true;
                getMore();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.shouldRefresh = false;
        if (firstVisibleItem + visibleItemCount == totalItemCount) {
            if (hasMore()) {
                if (listView.getFooterViewsCount() == 0 && footView != null) {
                    footView.setVisibility(View.VISIBLE);
                    listView.addFooterView(footView);
                }
                this.shouldRefresh = true;
            }
        }
    }

    public class DataAdapter extends BaseAdapter {

        public DataAdapter() {
        }

        @Override
        public int getCount() {
            int size = 0;
            if (inerEntries != null) {
                size += inerEntries.size();
            }
            return size;
        }

        public int getItemCount() {
            if (inerEntries != null) {
                return inerEntries.size();
            }
            return 0;
        }

        public boolean hasItems() {
            return inerEntries != null && !inerEntries.isEmpty();
        }

        @Override
        public boolean isEmpty() {
            return getCount() == 0;
        }

        @Override
        public Object getItem(int position) {
            if (inerEntries == null) {
                return null;
            }
            return inerEntries.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public boolean isEnabled(int position) {
            return true;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }


        public final View getView(int position, View convertView, ViewGroup parent) {
            return getItemView(position, convertView, parent);
        }


        @Override
        public int getItemViewType(int position) {
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }
    }

    protected abstract void addListHeader();

    protected abstract void addListFooter();

    protected abstract boolean isDataSame(T t1, T t2);

    protected abstract View getItemView(int position, View convertView, ViewGroup parent);

    protected abstract boolean hasMore();

    protected abstract void getMore();
}
